package cn.itsource.org.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.EmailService;
import cn.itsource.basic.util.PetConstant;
import cn.itsource.basic.util.RedisService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service //spring管理
public class ShopServiceImpl extends BaseServiceImpl<Shop, ShopQuery> implements IShopService {

    //基础的curd全部通过继承得到

    //因为事务都是在service层操作的
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmailService emailService;
    @Autowired
    private RedisService redisService;


    @Override
    public AjaxResult active(String uuid,Long id) {
        //0.如果已经激活过了，告诉他不用再激活了
        Shop shop1 = shopMapper.selectByPrimaryKey(id);
        if (shop1 != null && shop1.getState() == PetConstant.Shop.STATE_ACTIVE_SUCCESS){
            throw new RuntimeException("不要重复激活");
        }


        //1.从redis里面通过uuid获取店铺id
        String shopId = redisService.getKeyStringValue(uuid);
        if (StrUtil.isBlank(shopId)){
            throw new RuntimeException("激活时间已经过了，请联系管理员");
        }

        //2.激活
        Shop shop = new Shop();
        shop.setId(Long.valueOf(shopId));
        shop.setState(PetConstant.Shop.STATE_ACTIVE_SUCCESS);
        //修改状态
        shopMapper.updateByPrimaryKeySelective(shop);

        return AjaxResult.createSuccess("恭喜你，激活成功，可以登录系统了");
    }


    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @Override
    @Transactional //事务管理
    public AjaxResult settlement(Shop shop) {
        //先写主体，再写细节
        //验证规则自己去加  验证密码
        //1.新增员工到数据库
        Employee admin = shop.getAdmin();//没有id
        if (!StrUtil.equals(admin.getPassword(),admin.getComfirmPassword())){
            throw new RuntimeException("两次密码不一致");
        }
        //1.1判断用户名，手机号，邮箱全部都不能重复
        Employee employee = employeeService.checkRepeatEmployee(admin);
        if (employee != null){
            throw new RuntimeException("请检查用户名，手机号，邮箱有可能有一个重复了");
        }

        //uuid生成盐值
        /*String salt = IdUtil.fastSimpleUUID();
        String pwd = MD5.create().setSalt(salt.getBytes()).digestHex(admin.getPassword());
        //盐值和加密之后的密码入库
        admin.setSalt(salt);
        admin.setPassword(pwd);*/

        //调用钩子函数
        admin.md5Pwd();

        employeeService.save(admin);

        //2.拿到员工的自增长id，设置到店铺对象中
        shop.setAdmin(admin);//这个admin有自增id了
        //3.新增店铺信息
        shopMapper.insertShop(shop);

        //4.拿到店铺的id，设置员工，修改员工
        admin.setShopId(shop.getId());
        employeeService.update(admin);


        String uuid = IdUtil.fastSimpleUUID();
        //5.发送邮件
        emailService.sendEMail("宠物乐园激活",
                "Congratulations,审核通过,请点击链接激活,http://localhost/shop/jihuo/"+uuid+"/"+shop.getId(),admin.getEmail());
        //5.1redis存数据 key:uuid  value：店铺id
        redisService.setStringKeyAndValue(uuid,shop.getId(),1);

        //直接返回成功，如果异常会走全局异常
        return AjaxResult.createSuccess();
    }


    public static void main(String[] args) {
        String s = MD5.create().setSalt("sdlkfjsdilkjfsdlk".getBytes()).digestHex("123456");
        String s1 = MD5.create().setSalt("35e5490d775848ce9487d97a6e7af1da".getBytes()).digestHex("123");
        System.out.println(s);
        System.out.println(s1);

        String aaa = IdUtil.fastSimpleUUID();
        System.out.println(aaa);

    }

}
